using System.Linq;
using UnityEditor;

namespace GameFramework.Editor.Utility
{
    /// <summary>
    /// 脚本宏定义工具
    /// </summary>
    public static class ScriptingDefineSymbolsUtility
    {
        /// <summary>
        /// 添加宏定义
        /// </summary>
        public static void Add(string symbol)
        {
            PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, out var defines);
            if (defines.Contains(symbol))
                return;

            defines = defines.Concat(new[] { symbol }).ToArray();
            PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, defines);
        }

        /// <summary>
        /// 移除宏定义
        /// </summary>
        public static void Remove(string symbol)
        {
            PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, out var defines);
            if (!defines.Contains(symbol))
                return;

            defines = defines.Where(s => s != symbol).ToArray();
            PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, defines);
        }

        /// <summary>
        /// 切换宏定义
        /// </summary>
        public static void Toggle(string symbol)
        {
            PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, out var defines);
            if (defines.Contains(symbol))
            {
                defines = defines.Where(s => s != symbol).ToArray();
                PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, defines);
            }
            else
            {
                defines = defines.Concat(new[] { symbol }).ToArray();
                PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, defines);
            }
        }

        /// <summary>
        /// 是否包含宏定义
        /// </summary>
        public static bool Contains(string symbol)
        {
            PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, out var defines);
            return defines.Contains(symbol);
        }
    }
}